<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #login-div {
            width: 500px;
            height: 500px;
            border: 1px solid #ff5d39;
            position: relative;
        }

        h1 {
            text-align: center;
        }

        .input {
            height: 50px;
            width: 100%;
            border: 1px solid #ccc;
            border-radius: 6px;
            margin: 20px auto;
            color: #ccc;
            line-height: 50px;
            text-align: left;
        }

        .input-left {
            border-right: 1px solid #ccc;
            text-align: center;
            width: 100px;
            display: inline-block;
        }

        .login-btn {
            /*margin: 0 auto;*/
            display: inline-block;
            width: 160px;
            height: 50px;
            background-color: #4BC065;
            color: #fff;
            line-height: 50px;
            text-align: center;
            border-radius: 6px;
        }

        #captcha-box {
            position: absolute;
            left: 78px;
            top: 83px;
        }
    </style>
</head>

<body>
<!-- 验证码存放的div块 -->
<div id="login-div">
    <!-- 装载验证码的DIV -->
    <div id="captcha-box"></div>
    <h1>用户登录</h1>
    <div class="input">
        <span class="input-left">用户名</span>
        xxxxx
    </div>
    <div class="input">
        <span class="input-left">密码</span>
        xxxxx
    </div>
    <div class="login-btn">登录(图标点选)</div>
</div>

<script>
	function getCaptcha(){
		return fetch('/api/passport/get_captcha',{
			method:'POST',
			headers:{
				'Content-Type': 'application/json;charset=UTF-8',
			}
		}).then(res=>{
			 if (!res.ok) {
			    // 如果响应状态码不是 2xx，抛出错误
			    return Promise.reject(new Error('Request failed with status: ' + res.status));
			}
			return res.json();
		})
	}
	
	function sendAnswer(id,data,c,tac){
		console.log("验证函数被调用")
		const req= fetch("/api/passport/captcha",{
			method:'POST',
			headers:{
				'Content-Type': 'application/json;charset=UTF-8',
			},
			body: JSON.stringify({
				token: '123456',
				codeId: id,
				datas:data
			})
		}).then(res=>{
			if (!res.ok) {
			    // 如果响应状态码不是 2xx，抛出错误
			    return Promise.reject(new Error('Request failed with status: ' + res.status));
			}
			return res.json();
		})
		req.then(res=>{
			if(res.status==200){
				if(res.data){
					const useTimes = (data.stopTime - data.startTime) / 1000;
					c.showTips(`验证成功,耗时${useTimes}秒`,1)
					setTimeout(()=>{
						tac.destroyWindow()
					},3000)
					
				}else {
					c.showTips("验证失败，请重新尝试!", 0);
					setTimeout(()=>{
						tac.btnRefreshFun()//触发刷新
					},2000)
					
				}
			}else{
				c.showTips("验证码被黑洞吸走了！", 0);
				setTimeout(()=>{
					tac.btnRefreshFun()//触发刷新
				},2000)
			}
		})
	}
	function btnRefreshFun(tac){
		getCaptcha().then(data=>{
			if(data.status==200){
				tac.pushData(data.data)
			}
		})
	}
	
	const config = {
		bindEl: "#captcha-box",
		validCaptcha:this.sendAnswer,
		btnRefreshFun:this.btnRefreshFun,
		btnCloseFun:undefined
	}
	
	document.querySelectorAll(".login-btn").forEach(el => {
        el.addEventListener("click", e => {
			const captcha = new TAC(config, null);
			console.log(captcha)
			this.getCaptcha().then(data=>{
				if(data.status==200){
					captcha.init();
					captcha.pushData(data.data)
					// captcha.destroyCaptcha()
				}
			})
        })
    })
</script>
</body>

</html>
